﻿/*
 * Global custom js functions.
 */
var messageList = null; // message list plugin.

/*
 * Add a querystring value to a url.
 */
function addToUrl(url, param, value) {
    var separator = '&';

    url = url;

    if (url.indexOf('?') < 0)
        separator = '?'

    var remove = false;

    if (!value)
        remove = true;

    // test if param already exists.
    if (url.indexOf(param) > 0) {
        var start = url.indexOf(param) + param.length + 1;
        var end = url.indexOf('&', start);


        var newUrl;

        if (!remove) {

            newUrl = url.substring(0, start) + value;

            if (end && end > 0)
                newUrl = newUrl + url.substring(end);
        }
        else {
            newUrl = url.substring(0, start - param.length - 1);

            if (end && end > 0)
                newUrl = newUrl + url.substring(end);
        }

        return newUrl;
    }
    else {
        if (!remove)
            return url + separator + param + '=' + value;
        else
            return url;
    }
}

/*
* Process a generic ajax get request.
* If container is passed then the data returned is assumed to
* be html and inserted into the container.
* If a call back is passed then that will be executed on success.
*/
function doAjaxGet(url, container, callback, errorCallback, noWaiter) {
    if (!url)
        return;

    //ajaxProcessing = true;
    if (container && !noWaiter) {
        $(container).waiter();
    }

    // Add an extra random number to the parameters to overcome IE8 request cache bug
    var randNum =   Math.floor(Math.random()*100000000);
    url = addToUrl(url, "reqId", randNum);

    $.ajax({
        url: url,
        type: 'get',
        dataType: 'html',
        error: function (data) {
            if (container) {
                if (!noWaiter)
                    $(container).waiter('destroy');
            }
            
            if (errorCallback)
                errorCallback(data);
        },
        success: function (data) {
            if (container) {
                $(container).html(data);

                if (!noWaiter)
                    $(container).waiter('destroy');
            }
            else
                showMessage(data); // no container so assume return is just a message.

            if (callback)
                callback();
        },
        complete: null
    });
}

/*
* Process a generic ajax get request.
* Data returned as json.  Assumed to be a JsonResponse object.
* If a call back is passed then that will be executed on success.
*/
function doAjaxJsonGet(url, callback, errorCallback) {
    if (!url)
        return;

//    //ajaxProcessing = true;
//    if (container) {
//        $(container).waiter();
//    }

    $.ajax({
        url: url,
        type: 'get',
        dataType: 'json',
        error: errorCallback,
        success: function (data) {
            if (data.Status == '1') { // error so show message
                showErrorText(data.Message);
            }

            if (callback)
                callback(data);
        },
        complete: null
    });
}

function showWarning(data) {
    showErrorText(data.responseText);
}

function showWarningText(text) {
    if (!text || !messageList)
        return;

    messageList.push(text, 'warning');

/*    $('.message-box').html(text);
    $('.message-box').fadeIn(300);

    setTimeout(function () {
        $('.message-box').fadeOut(1500, function () {
            $('.message-box').html('');
        });
    }, 3000);*/
}

function showErrorText(text) {
    if (!text || !messageList)
        return;

    messageList.push(text, 'error');
}

function showMessage(html) {
    if (!html || !messageList)
        return;

    messageList.push(html, 'info');

//    $('.message-box').html('<span class="message">' + html + '</span>');
//    $('.message-box').fadeIn(300);

//    setTimeout(function () {
//        $('.message-box').fadeOut(1500, function () {
//            $('.message-box').html('');
//        });
//    }, 3000);
}

/*
* Validate if a value is a date.
* Date should be in dd/mm/yyyy format.
*/
function isValidDate(date) {
    var isValid = true;

    try {
        jQuery.datepicker.parseDate('dd/mm/yy', date, null);
    }
    catch (error) {
        isValid = false;
    }

    return isValid;
}

/*
* Validate a value is a number.
*/
function isValidNumber(number) {
    success = false;
    if (number) {
        var value = number.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
        var intRegex = /^\d+$/;
        if (intRegex.test(value)) {
            success = true;
        }
    }

    return success;
}


function setupMessageList() {
    var messageBox = $('.message-box').messagelist();
    messageList = messageBox.data('hqpadMessagelist');
}